package com.itlong.cloud.operate.service.impl;

import com.itlong.cloud.POJO.DTO.operate.OperateUserRecordPageDTO;
import com.itlong.cloud.POJO.VO.operate.OperateDeviceOpenTypeVO;
import com.itlong.cloud.POJO.VO.operate.OperateUserRecordVO;
import com.itlong.cloud.enumerate.PropertyEquipTypeEnum;
import com.itlong.cloud.operate.dao.IOperateUserRecordDao;
import com.itlong.cloud.operate.service.IOperateUserRecordService;
import com.itlong.cloud.utils.data.handler.SqlUtil;
import com.itlong.cloud.utils.page.Page;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * <desc>
 *      运营—APP用户-业务通行记录服务接口实现类。
 * <desc/>
 *
 * @createDate 2017/9/26.
 */
@Service
public class OperateUserRecordServiceImpl implements IOperateUserRecordService {

    private static final Logger LOG = LoggerFactory.getLogger(OperateUserRecordServiceImpl.class);

    @Autowired
    IOperateUserRecordDao iOperateAppUserRecordDao;
    /**
     * <desc>
     *      获取分页数据
     * </desc>
     *
     * @param pageDTO
     * @return
     * @author 2017/09/21
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
     public Page<OperateUserRecordVO> getPage(OperateUserRecordPageDTO pageDTO) throws Exception {
        try {
            Integer recType = pageDTO.getRecType();
            //将DTO转换为Map params
            Map<String, Object> params = SqlUtil.convertBeanToMapParams(pageDTO);
            //分页类默认pageSize为20
            Page<OperateUserRecordVO> page = new Page<>();
            page.getPage().setPageSize(pageDTO.getPageSize());
            page.getPage().setCurrentPage(pageDTO.getCurrentPage());
            params.put("currentPage",page.getPage().getStartSlip());
            params.put("pageSize",page.getPage().getPageSize());
            List<OperateUserRecordVO> recordList = null;
            Integer pageCount=0;
            //填充返回数据集  1电梯  2门禁 3 云电梯
            switch(PropertyEquipTypeEnum.getByEquipType(recType)) {
                case ELEVATOR:
                    //电梯没有qq和呼出号码
                    params.put("qqNum",null);
                    params.put("callPhoneNo",null);
                    //电梯通行记录
                    if(pageDTO.getRecTimeType()==2){
                        //查询三个月前的通行记录
                        recordList= iOperateAppUserRecordDao.getElevatorRecordListByHistory(params);
                        pageCount =iOperateAppUserRecordDao.getElevatorRecordCountByHistory(params);
                    }else{
                        recordList= iOperateAppUserRecordDao.getElevatorRecordList(params);
                        pageCount =iOperateAppUserRecordDao.getElevatorRecordCount(params);
                    }
                    break;
                case ENTRANCE:
                    //门禁没有qq和呼出号码
                    params.put("qqNum",null);
                    params.put("callPhoneNo",null);
                    //门禁通行记录
                    if(pageDTO.getRecTimeType()==2){
                        //查询三个月前的通行记录
                        recordList= iOperateAppUserRecordDao.getEntranceRecordListByHistory(params);
                        pageCount =iOperateAppUserRecordDao.getEntranceRecordCountByHistory(params);
                    }else{
                        recordList= iOperateAppUserRecordDao.getEntranceRecordList(params);
                        pageCount =iOperateAppUserRecordDao.getEntranceRecordCount(params);
                    }
                    break;
                case CLOUD_INTERCOM:
                    //云对讲通行记录
                    if(pageDTO.getRecTimeType()==2){
                        //查询三个月前的通行记录
                        recordList= iOperateAppUserRecordDao.getCloudIntercomRecordListByHistory(params);
                        pageCount =iOperateAppUserRecordDao.getCloudRecordCountByHistory(params);
                    }else{
                        recordList= iOperateAppUserRecordDao.getCloudIntercomRecordList(params);
                        pageCount =iOperateAppUserRecordDao.getCloudRecordCount(params);
                    }
                    break;
                default:
                   return null;
            }
            page.setRows(recordList,pageCount);
            return page;
        } catch (Exception e) {
            throw new Exception("运营_APP用户_获取业主通行记录分页数据异常：{}", e);
        }
    }
     /**
      * <desc>
      *      返回通行方式。
      * <desc/>
      *
      * @param recType
      * @return 返回通行方式列表
      * @author shengen.T
      * @createDate 2017/10/31
      */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public List<OperateDeviceOpenTypeVO> getOpenTypeList(Integer recType) throws Exception{
       return iOperateAppUserRecordDao.getOpenTypeList(recType);
    }

}
